package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.CarMapper;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.mapper.OrderMapper;
import com.ybkj.daijia.server.mapper.PeccancyAndComplainMapper;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.PeccancyAndComplain;
import com.ybkj.daijia.server.service.PeccancyAndComplainService;
import com.ybkj.daijia.server.service.SuperviceService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Car;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 违章与投诉
 *
 * @author Administrator
 */
@Service
public class DefaultPeccancyAndComplainService implements PeccancyAndComplainService {

    @Autowired
    private PeccancyAndComplainMapper peccancyAndComplainMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private SuperviceService superviceService;


    @Override
    public void insert(PeccancyAndComplain peccancyAndComplain) {

        peccancyAndComplainMapper.insertRecord(peccancyAndComplain);

        //上传司机信誉信息
        superviceService.SjxxMessage(peccancyAndComplain);
        //上传司机处罚信息
        superviceService.CfxxMessage(peccancyAndComplain);
        //如果是投诉，上传乘客投诉接口信息
        if ("投诉".equals(peccancyAndComplain.getComplainType())) {
            superviceService.CkpjMessage(peccancyAndComplain);
        }
    }

    @Override
    public Page<PeccancyAndComplain> queryByEmploy(Long driverId) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        List<PeccancyAndComplain> content = null;
        try {
            content = peccancyAndComplainMapper
                .queryByEmploy(driverId, Pager.getOffset(), Pager.getLimit());
        } catch (Exception e) {
            e.printStackTrace();

        }
        for (PeccancyAndComplain peccancyAndComplain : content) {

            //关联对应的车牌号
            Driver driver = driverMapper.selectByPrimaryKey(driverId);

            Long carId = driver.getCarId();
            Car car = zhuancheService.findCar(carId);
            peccancyAndComplain.setCarNumber(car.getCarNumber());
        }

        Long total = peccancyAndComplainMapper.countByEmploy(driverId);

        return new PageImpl<PeccancyAndComplain>(content, pageable, total);
    }

}
